package controller.ucartc;

import com.zuche.framework.utils.HttpConnUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by PCG on 2016/9/26.
 */
@RequestMapping(value = "epospay/",produces = "text/html;charset=utf-8")
@Controller
public class EposPayController {

    String gateWay = "https://tcpre.10101111.com/tc-gateway/";

    String interFace1 = "/epospay/pay";

    @RequestMapping(value = "topay.do_",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toPay(HttpServletRequest request,HttpServletResponse response){
        String result = toHtmlPay();
        return result;

    }


    private String toHtmlPay(){
        Map<String,String> params = new TreeMap<String, String>();
        params.put("b_accName","eposmmcused");
        params.put("b_busiId","tc-test-webpay-alipay");
        params.put("b_money","0.01");
        params.put("b_notifyUrl","http://www.maimaiche.com/pay/returnpage.do");
        params.put("b_sourceId","epospay");
        params.put("b_desc","szmmcnew");
        params.put("e_aliType","1");//1：wap
        params.put("e_client_ip","127.0.0.1");
        params.put("b_traderId","11111111111");
        params.put("e_payer_name","雷霆");
        params.put("e_card_no","6217920660398566");
        params.put("e_payer_tel","15301067848");
        params.put("e_identity_type","0");
        params.put("e_identity_id","652323199107043516");
        params.put("e_is_validate","false");
        params.put("tc_customized_order_id",new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()));
        String sign = getSign("q1w2e3r4t5y6u7i8o9p0",params);
        String url = "";
        for (Map.Entry<String,String> entry:params.entrySet()){
            String name = entry.getKey();
            url += name + "=" + entry.getValue() + "&";
        }
        url += "b_sign="+sign;

        HttpsUtils hs = new HttpsUtils();
        return hs.postConnection(gateWay + interFace1, url);
    }

    public String getSign(String key,Map<String,String> paramMap){

        TreeMap<String,String> signMap = new TreeMap<String,String>();
        for (String paramKey : paramMap.keySet()) {
            signMap.put(paramKey, paramMap.get(paramKey));
        }
        String content = "";
        for (String keyName : signMap.keySet()) {
            content += keyName + "=" + signMap.get(keyName) + ";";
        }
        return DigestUtils.md5Hex(content + "key=" + key);

    }

    @RequestMapping("tc/callback.do_")
    public String callBack(HttpServletRequest request,HttpServletResponse response) throws Exception{
        OutputStream output = response.getOutputStream();
        output.write("success".getBytes());
        return "jsp/testSuccess.jsp";

    }

    @RequestMapping(value = "tc/testEncode.do_",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String testEncode() throws Exception{
        String url = "http://127.0.0.1:8083/cartptest/tc/testreturnpage.do_";
        Map<String,String> params = new HashMap<String, String>();
        params.put("aaa","aaaa123");
        HttpURLConnection conn = HttpConnUtils.getPostHttpConn(url, params);
        conn.connect();
        InputStream in = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
        String clientInfo = reader.readLine();
        String clientFlag = conn.getHeaderField("CAR_TC");

        return "<html><a href='#'>哈哈哈</a>"+clientFlag+clientInfo+"</html>";
    }

    @RequestMapping("tc/testreturnpage.do_")
    public String testReturnPage(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //加入固定标记，通知交易平台回调成功
        response.addHeader("CAR_TC", Boolean.TRUE.toString());
        return "/jsp/testSuccess.jsp";
    }

}
